package com.bayern.xml.example;

import java.nio.charset.Charset;

import com.bayern.factory.SecureFactory;
import com.bayern.other.SecureType;
import com.bayern.secure.Base64Utils;
import com.bayern.secure.DESCodec;
import com.bayern.secure.MD5Codec;
import com.bayern.xml.example.entity.req.BasicReq;
import com.bayern.xml.example.entity.req.StaffBindReqBody;
import com.bayern.xml.transform.XmlTransformUtils;

public class Demo {

	public static void main(String[] args) throws Exception
	{
		String DESKey = "wu8IHObx1WE=";
		MD5Codec md5Codec = (MD5Codec)SecureFactory.getCodec(SecureType.MD5, null);
		DESCodec desCodec = (DESCodec)SecureFactory.getCodec(SecureType.DES, DESKey);
		String bodyXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Body><CORPACCOUNT>2901</CORPACCOUNT><STAFFS><UFID>25</UFID><STAFFNAME>tomas</STAFFNAME><STAFFMOBILE>25252525001</STAFFMOBILE><USERTYPE>0</USERTYPE><USERINFOMAP><USERINFONAME>用户信息参数名称</USERINFONAME><USERINFOVALUE>用户信息参数值</USERINFOVALUE></USERINFOMAP><OPTYPE>1</OPTYPE></STAFFS><STAFFS><UFID>21</UFID><STAFFNAME>lamu</STAFFNAME><STAFFMOBILE>21212121001</STAFFMOBILE><USERTYPE>1</USERTYPE><USERINFOMAP><USERINFONAME>用户信息参数名称</USERINFONAME><USERINFOVALUE>用户信息参数值</USERINFOVALUE></USERINFOMAP><OPTYPE>2</OPTYPE></STAFFS></Body>";
		String md5 = md5Codec.getEncryptForHex(bodyXml.getBytes("UTF-8"));
//		System.out.println("bodyXml:" + bodyXml);
//		System.out.println("md5:" + md5);
		String bodyContext = md5 + bodyXml;
		byte[] data = desCodec.encrypt(bodyContext.getBytes());
		String bodyBase64 = Base64Utils.getInstance().encoder(data);
//		System.out.println("bodyBase64:" + bodyBase64);
		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><StaffBindReq><HEAD><CODE>消息标志</CODE><SID>消息序列号</SID><TIMESTAMP>时间戳</TIMESTAMP><SERVICEID>业务应用标识</SERVICEID></HEAD><BODY>" + bodyBase64 + "</BODY></StaffBindReq>";
//		Document document = DocumentHelper.parseText(xml);
//		Element rootElement = document.getRootElement();
		BasicReq basicReq = (BasicReq)XmlTransformUtils.xmlTransformObj(BasicReq.class, xml);
		System.out.println("=================解析==================");
		String decryptXml = XmlTransformUtils.objTransformXml(BasicReq.class, basicReq);
		System.out.println("===decryptXml:" + decryptXml);
		
		
		System.out.println("=================编码==================");
		String bodyEncoder = basicReq.getBody();
		System.out.println("bodyEncoder:" + bodyEncoder);
		byte[] bodyDecoder = Base64Utils.getInstance().decoder(bodyEncoder);
		bodyContext = new String(desCodec.decrypt(bodyDecoder), Charset.forName("UTF-8"));
//		System.out.println("parseToStaffBindReq===========bodyContext:" + bodyContext);
		md5 = bodyContext.substring(0, 32);
//		System.out.println("parseToStaffBindReq===========md5:" + md5);
		bodyXml = bodyContext.substring(32);
//		Document bodyDocument = DocumentHelper.parseText(bodyXml);
//		Element bodyRootElement = bodyDocument.getRootElement();
		StaffBindReqBody staffBindBody = (StaffBindReqBody)XmlTransformUtils.xmlTransformObj(StaffBindReqBody.class, bodyXml);
		System.out.println(staffBindBody);
		System.out.println("=================解析==================");
		decryptXml = XmlTransformUtils.objTransformXml(StaffBindReqBody.class, staffBindBody);
		System.out.println("===decryptXml:" + decryptXml);
		
	}
}
